package com.ai.bitcoin.weixin;

import com.ai.bitcoin.service.helper.WeiXinPayServiceHelper.WeixinCallBackResult;
import org.apache.commons.lang3.StringUtils;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Map;


/**
 * 
 * @author xd
 *
 */
public class WeiXinCallBackDom extends BaseDom<WeixinCallBackResult> {
	
	private static final Logger logger = LoggerFactory.getLogger(WeiXinCallBackDom.class);

	@Override
	public WeixinCallBackResult parserXml(String xml) {
		if (StringUtils.isBlank(xml)) {
			throw new NullPointerException("xml is emty");
		}

		SAXBuilder saxBuilder = new SAXBuilder();
		saxBuilder.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
		saxBuilder.setFeature("http://xml.org/sax/features/external-general-entities", false);
		saxBuilder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
		Document document = null;
		
		try {
			document = saxBuilder.build(new StringReader(xml));
		} catch (JDOMException e) {
			logger.error("input dom string occured error", e);
		} catch (IOException e) {
			logger.error("input io occured error", e);
		}
		
		if (document != null) {
			Element root = document.getRootElement();
			if (root != null ) {
				List<Element> childrens = root.getChildren();
				if (childrens != null && !childrens.isEmpty()) {
					WeixinCallBackResult weixinCallBackResult = new WeixinCallBackResult();
					for (Element element : childrens) {
						String elementName = element.getName();
						switch (elementName) {
						case "appid":
							weixinCallBackResult.setAppid(element.getValue());
							break;
						case "bank_type":
							weixinCallBackResult.setBank_type(element.getValue());
							break;
						case "cash_fee":
							weixinCallBackResult.setCash_fee(Integer.valueOf(element.getValue()));
							break;
						case "fee_type":
							weixinCallBackResult.setFee_type(element.getValue());
							break;
						case "is_subscribe"	:
							weixinCallBackResult.setIs_subscribe(element.getValue());
							break;
						case "mch_id":	
							weixinCallBackResult.setMch_id(element.getValue());
							break;
						case "nonce_str":	
							weixinCallBackResult.setNonce_str(element.getValue());
							break;
						case "openid":
							weixinCallBackResult.setOpenid(element.getValue());
							break;
						case "out_trade_no":
							weixinCallBackResult.setOut_trade_no(element.getValue());
							break;
						case "result_code":
							weixinCallBackResult.setResult_code(element.getValue());
							break;
						case "return_code":
							weixinCallBackResult.setReturn_code(element.getValue());
							break;
						case "sign":
							weixinCallBackResult.setSign(element.getValue());
							break;
						case "time_end":
							weixinCallBackResult.setTime_end(element.getValue());
							break;
						case "total_fee":
							weixinCallBackResult.setTotal_fee(Integer.valueOf(element.getValue()));
							break;
						case "trade_type":	
							weixinCallBackResult.setTrade_type(element.getValue());
							break;
						case "transaction_id":
							weixinCallBackResult.setTransaction_id(element.getValue());
							break;
							
						case "err_code":
							weixinCallBackResult.setErr_code(element.getValue());
							break;
						case "err_code_des":
							weixinCallBackResult.setErr_code_des(element.getValue());
							break;
						case "return_msg":
							weixinCallBackResult.setReturn_msg(element.getValue());
						    break;
						
						//add weixin coupon field
						 case "coupon_count":
							 weixinCallBackResult.setCoupon_count(Integer.valueOf(element.getValue()));
						     break;
						 case "coupon_fee":
							 weixinCallBackResult.setCoupon_fee(Integer.valueOf(element.getValue()));
							 break;
						 case "coupon_id_0":
							 weixinCallBackResult.setCoupon_id_0(element.getValue());
							 break;
						 case "coupon_id_1":
							 weixinCallBackResult.setCoupon_id_1(element.getValue());
							 break;
						 case "coupon_id_2":
							 weixinCallBackResult.setCoupon_id_2(element.getValue());
							 break;
						 case "coupon_id_3":
							 weixinCallBackResult.setCoupon_id_3(element.getValue());
							 break;
						 case "coupon_id_4":
							 weixinCallBackResult.setCoupon_id_4(element.getValue());
							 break;
							 
						 case "coupon_fee_0":
							 weixinCallBackResult.setCoupon_fee_0(Integer.valueOf(element.getValue()));
							 break;
						 case "coupon_fee_1":
							 weixinCallBackResult.setCoupon_fee_1(Integer.valueOf(element.getValue()));
							 break;
						 case "coupon_fee_2":
							 weixinCallBackResult.setCoupon_fee_2(Integer.valueOf(element.getValue()));
							 break;
						 case "coupon_fee_3":
							 weixinCallBackResult.setCoupon_fee_3(Integer.valueOf(element.getValue()));
							 break;
						 case "coupon_fee_4":
							 weixinCallBackResult.setCoupon_fee_4(Integer.valueOf(element.getValue()));
							 break;

//							case "out_refund_no" :
//								weixinCallBackResult.setOut_refund_no(element.getValue());
//							case "refund_id" :
//								weixinCallBackResult.setRefund_id(element.getValue());
//							case "refund_fee" :
//								weixinCallBackResult.setRefund_fee(element.getValue());
						default:
							break;
						}
						
					}
					
					return weixinCallBackResult;
				}
				
			}
			
		}
		
		return null;
	}

	@Override
	public String createXml(Map<String, Object> map) {
		return null;
	}
	

}
